home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / macros / texinfo / info / makedoc.c < prev    next >
C/C++ Source or Header  |  1994-01-28  |  12KB  |  478 lines

  1. /* makedoc.c -- Make DOC.C and FUNS.H from input files. */
  2.  
  3. /* This file is part of GNU Info, a program for reading online documentation
  4.    stored in Info format.
  5.  
  6.    Copyright (C) 1993 Free Software Foundation, Inc.
  7.  
  8.    This program is free software; you can redistribute it and/or modify
  9.    it under the terms of the GNU General Public License as published by
  10.    the Free Software Foundation; either version 2, or (at your option)
  11.    any later version.
  12.  
  13.    This program is distributed in the hope that it will be useful,
  14.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.    GNU General Public License for more details.
  17.  
  18.    You should have received a copy of the GNU General Public License
  19.    along with this program; if not, write to the Free Software
  20.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  
  22.    Written by Brian Fox (bfox@ai.mit.edu). */
  23.  
  24. /* This program grovels the contents of the source files passed as arguments
  25.    and writes out a file of function pointers and documentation strings, and
  26.    a header file which describes the contents.  This only does the functions
  27.    declared with DECLARE_INFO_COMMAND. */
  28.  
  29. #include <stdio.h>
  30. #include <ctype.h>
  31. #include <sys/types.h>
  32. #include <sys/file.h>
  33. #include <sys/stat.h>
  34. #include "general.h"
  35.  
  36. #if !defined (O_RDONLY)
  37. #if defined (HAVE_SYS_FCNTL_H)
  38. #include <sys/fcntl.h>
  39. #else /* !HAVE_SYS_FCNTL_H */
  40. #include <fcntl.h>
  41. #endif /* !HAVE_SYS_FCNTL_H */
  42. #endif /* !O_RDONLY */
  43.  
  44. extern void *xmalloc (), *xrealloc ();
  45. static void fatal_file_error ();
  46.  
  47. /* Name of the header file which receives the declarations of functions. */
  48. static char *funs_filename = "funs.h";
  49.  
  50. /* Name of the documentation to function pointer file. */
  51. static char *doc_filename = "doc.c";
  52.  
  53. static char *doc_header[] = {
  54.   "/* doc.c -- Generated structure containing function names and doc strings.",
  55.   "",
  56.   "   This file was automatically made from various source files with the",
  57.   "   command \"%s\".  DO NOT EDIT THIS FILE, only \"%s.c\".",
  58.   (char *)NULL
  59. };
  60.  
  61. static char *doc_header_1[] = {
  62.   "   An entry in the array FUNCTION_DOC_ARRAY is made for each command",
  63.   "   found in the above files; each entry consists of a function pointer,",
  64. #if defined (NAMED_FUNCTIONS)
  65.   "   a string which is the user-visible name of the function,",
  66. #endif /* NAMED_FUNCTIONS */
  67.   "   and a string which documents its purpose. */",
  68.   "",
  69.   "#include \"doc.h\"",
  70.   "#include \"funs.h\"",
  71.   "",
  72.   "FUNCTION_DOC function_doc_array[] = {",
  73.   "",
  74.   (char *)NULL
  75. };
  76.  
  77. /* How to remember the locations of the functions found so that Emacs
  78.    can use the information in a tag table. */
  79. typedef struct {
  80.   char *name;            /* Name of the tag. */
  81.   int line;            /* Line number at which it appears. */
  82.   long char_offset;        /* Character offset at which it appears. */
  83. } EMACS_TAG;
  84.  
  85. typedef struct {
  86.   char *filename;        /* Name of the file containing entries. */
  87.   long entrylen;        /* Total number of characters in tag block. */
  88.   EMACS_TAG **entries;        /* Entries found in FILENAME. */
  89.   int entries_index;
  90.   int entries_slots;
  91. } EMACS_TAG_BLOCK;
  92.  
  93. EMACS_TAG_BLOCK **emacs_tags = (EMACS_TAG_BLOCK **)NULL;
  94. int emacs_tags_index = 0;
  95. int emacs_tags_slots = 0;
  96.  
  97. #define DECLARATION_STRING "\nDECLARE_INFO_COMMAND"
  98.  
  99. static void process_one_file ();
  100. static void maybe_dump_tags ();
  101. static FILE *must_fopen ();
  102.  
  103. int
  104. main (argc, argv)
  105.      int argc;
  106.      char **argv;
  107. {
  108.   register int i;
  109.   int tags_only = 0;
  110.   FILE *funs_stream, *doc_stream;
  111.  
  112.   for (i = 1; i < argc; i++)
  113.     if (strcmp (argv[i], "-tags") == 0)
  114.       {
  115.     tags_only++;
  116.     break;
  117.       }
  118.  
  119.   if (tags_only)
  120.     {
  121.       funs_filename = "/dev/null";
  122.       doc_filename = "/dev/null";
  123.     }
  124.   
  125.   funs_stream = must_fopen (funs_filename, "w");
  126.   doc_stream = must_fopen (doc_filename, "w");
  127.  
  128.   fprintf (funs_stream,
  129.        "/* %s -- Generated declarations for Info commands. */\n",
  130.        funs_filename);
  131.  
  132.   for (i = 0; doc_header[i]; i++)
  133.     {
  134.       fprintf (doc_stream, doc_header[i], argv[0], argv[0]);
  135.       fprintf (doc_stream, "\n");
  136.     }
  137.  
  138.   fprintf (doc_stream,
  139.        "   Source files groveled to make this file include:\n\n");
  140.  
  141.   for (i = 1; i < argc; i++)
  142.     fprintf (doc_stream, "\t%s\n", argv[i]);
  143.  
  144.   fprintf (doc_stream, "\n");
  145.  
  146.   for (i = 0; doc_header_1[i]; i++)
  147.     fprintf (doc_stream, "%s\n", doc_header_1[i]);
  148.  
  149.  
  150.   for (i = 1; i < argc; i++)
  151.     {
  152.       char *curfile;
  153.       curfile = argv[i];
  154.  
  155.       if (*curfile == '-')
  156.     continue;
  157.  
  158.       fprintf (doc_stream, "/* Commands found in \"%s\". */\n", curfile);
  159.       fprintf (funs_stream, "\n/* Functions declared in \"%s\". */\n",
  160.            curfile);
  161.  
  162.       process_one_file (curfile, doc_stream, funs_stream);
  163.     }
  164.  
  165.   fprintf (doc_stream,
  166.        "   { (VFunction *)NULL, (char *)NULL, (char *)NULL }\n};\n");
  167.  
  168.   fclose (funs_stream);
  169.   fclose (doc_stream);
  170.  
  171.   if (tags_only)
  172.     maybe_dump_tags (stdout);
  173.   exit (0);
  174. }
  175.  
  176. /* Dumping out the contents of an Emacs tags table. */
  177. static void
  178. maybe_dump_tags (stream)
  179.      FILE *stream;
  180. {
  181.   register int i;
  182.  
  183.   /* Print out the information for each block. */
  184.   for (i = 0; i < emacs_tags_index; i++)
  185.     {
  186.       register int j;
  187.       register EMACS_TAG_BLOCK *block;
  188.       register EMACS_TAG *etag;
  189.       long block_len;
  190.  
  191.       block_len = 0;
  192.       block = emacs_tags[i];
  193.  
  194.       /* Calculate the length of the dumped block first. */
  195.       for (j = 0; j < block->entries_index; j++)
  196.     {
  197.       char digits[30];
  198.       etag = block->entries[j];
  199.       block_len += 3 + strlen (etag->name);
  200.       sprintf (digits, "%d,%d", etag->line, etag->char_offset);
  201.       block_len += strlen (digits);
  202.     }
  203.  
  204.       /* Print out the defining line. */
  205.       fprintf (stream, "\f\n%s,%d\n", block->filename, block_len);
  206.  
  207.       /* Print out the individual tags. */
  208.       for (j = 0; j < block->entries_index; j++)
  209.     {
  210.       etag = block->entries[j];
  211.  
  212.       fprintf (stream, "%s,\177%d,%d\n",
  213.            etag->name, etag->line, etag->char_offset);
  214.     }
  215.     }
  216. }
  217.  
  218. /* Keeping track of names, line numbers and character offsets of functions
  219.    found in source files. */
  220. static EMACS_TAG_BLOCK *
  221. make_emacs_tag_block (filename)
  222.      char *filename;
  223. {
  224.   EMACS_TAG_BLOCK *block;
  225.  
  226.   block = (EMACS_TAG_BLOCK *)xmalloc (sizeof (EMACS_TAG_BLOCK));
  227.   block->filename = savestring (filename);
  228.   block->entrylen = 0;
  229.   block->entries = (EMACS_TAG **)NULL;
  230.   block->entries_index = 0;
  231.   block->entries_slots = 0;
  232.   return (block);
  233. }
  234.  
  235. static void
  236. add_tag_to_block (block, name, line, char_offset)
  237.      EMACS_TAG_BLOCK *block;
  238.      char *name;
  239.      int line;
  240.      long char_offset;
  241. {
  242.   EMACS_TAG *tag;
  243.  
  244.   tag = (EMACS_TAG *)xmalloc (sizeof (EMACS_TAG));
  245.   tag->name = name;
  246.   tag->line = line;
  247.   tag->char_offset = char_offset;
  248.   add_pointer_to_array (tag, block->entries_index, block->entries,
  249.             block->entries_slots, 50, EMACS_TAG *);
  250. }
  251.  
  252. /* Read the file represented by FILENAME into core, and search it for Info
  253.    function declarations.  Output the declarations in various forms to the
  254.    DOC_STREAM and FUNS_STREAM. */
  255. static void
  256. process_one_file (filename, doc_stream, funs_stream)
  257.      char *filename;
  258.      FILE *doc_stream, *funs_stream;
  259. {
  260.   int descriptor, decl_len;
  261.   char *buffer, *decl_str;
  262.   struct stat finfo;
  263.   long offset;
  264.   EMACS_TAG_BLOCK *block;
  265.  
  266.   if (stat (filename, &finfo) == -1)
  267.     fatal_file_error (filename);
  268.  
  269.   descriptor = open (filename, O_RDONLY, 0666);
  270.  
  271.   if (descriptor == -1)
  272.     fatal_file_error (filename);
  273.  
  274.   buffer = (char *)xmalloc (1 + finfo.st_size);
  275.   read (descriptor, buffer, finfo.st_size);
  276.   close (descriptor);
  277.  
  278.   offset = 0;
  279.   decl_str = DECLARATION_STRING;
  280.   decl_len = strlen (decl_str);
  281.  
  282.   block = make_emacs_tag_block (filename);
  283.  
  284.   while (1)
  285.     {
  286.       long point = 0;
  287.       long line_start = 0;
  288.       int line_number = 0;
  289.  
  290.       char *func, *doc;
  291. #if defined (NAMED_FUNCTIONS)
  292.       char *func_name;
  293. #endif /* NAMED_FUNCTIONS */
  294.  
  295.       for (; offset < (finfo.st_size - decl_len); offset++)
  296.     {
  297.       if (buffer[offset] == '\n')
  298.         {
  299.           line_number++;
  300.           line_start = offset + 1;
  301.         }
  302.  
  303.       if (strncmp (buffer + offset, decl_str, decl_len) == 0)
  304.         {
  305.           offset += decl_len;
  306.           point = offset;
  307.           break;
  308.         }
  309.     }
  310.  
  311.       if (!point)
  312.     break;
  313.  
  314.       /* Skip forward until we find the open paren. */
  315.       while (point < finfo.st_size)
  316.     {
  317.       if (buffer[point] == '\n')
  318.         {
  319.           line_number++;
  320.           line_start = point + 1;
  321.         }
  322.       else if (buffer[point] == '(')
  323.         break;
  324.  
  325.       point++;
  326.     }
  327.  
  328.       while (point++ < finfo.st_size)
  329.     {
  330.       if (!whitespace_or_newline (buffer[point]))
  331.         break;
  332.       else if (buffer[point] == '\n')
  333.         {
  334.           line_number++;
  335.           line_start = point + 1;
  336.         }
  337.     }
  338.  
  339.       if (point >= finfo.st_size)
  340.     break;
  341.  
  342.       /* Now looking at name of function.  Get it. */
  343.       for (offset = point; buffer[offset] != ','; offset++);
  344.       func = (char *)xmalloc (1 + (offset - point));
  345.       strncpy (func, buffer + point, offset - point);
  346.       func[offset - point] = '\0';
  347.  
  348.       /* Remember this tag in the current block. */
  349.       {
  350.     char *tag_name;
  351.  
  352.     tag_name = (char *)xmalloc (1 + (offset - line_start));
  353.     strncpy (tag_name, buffer + line_start, offset - line_start);
  354.     tag_name[offset - line_start] = '\0';
  355.     add_tag_to_block (block, tag_name, line_number, point);
  356.       }
  357.  
  358. #if defined (NAMED_FUNCTIONS)
  359.       /* Generate the user-visible function name from the function's name. */
  360.       {
  361.     register int i;
  362.     char *name_start;
  363.  
  364.     name_start = func;
  365.  
  366.     if (strncmp (name_start, "info_", 5) == 0)
  367.       name_start += 5;
  368.  
  369.     func_name = savestring (name_start);
  370.  
  371.     /* Fix up "ea" commands. */
  372.     if (strncmp (func_name, "ea_", 3) == 0)
  373.       {
  374.         char *temp_func_name;
  375.  
  376.         temp_func_name = (char *)xmalloc (10 + strlen (func_name));
  377.         strcpy (temp_func_name, "echo_area_");
  378.         strcat (temp_func_name, func_name + 3);
  379.         free (func_name);
  380.         func_name = temp_func_name;
  381.       }
  382.  
  383.     for (i = 0; func_name[i]; i++)
  384.       if (func_name[i] == '_')
  385.         func_name[i] = '-';
  386.       }
  387. #endif /* NAMED_FUNCTIONS */
  388.  
  389.       /* Find doc string. */
  390.       point = offset + 1;
  391.  
  392.       while (point < finfo.st_size)
  393.     {
  394.       if (buffer[point] == '\n')
  395.         {
  396.           line_number++;
  397.           line_start = point + 1;
  398.         }
  399.  
  400.       if (buffer[point] == '"')
  401.         break;
  402.       else
  403.         point++;
  404.     }
  405.  
  406.       offset = point + 1;
  407.  
  408.       while (offset < finfo.st_size)
  409.     {
  410.       if (buffer[offset] == '\n')
  411.         {
  412.           line_number++;
  413.           line_start = offset + 1;
  414.         }
  415.  
  416.       if (buffer[offset] == '\\')
  417.         offset += 2;
  418.       else if (buffer[offset] == '"')
  419.         break;
  420.       else
  421.         offset++;
  422.     }
  423.  
  424.       offset++;
  425.       if (offset >= finfo.st_size)
  426.     break;
  427.  
  428.       doc = (char *)xmalloc (1 + (offset - point));
  429.       strncpy (doc, buffer + point, offset - point);
  430.       doc[offset - point] = '\0';
  431.  
  432. #if defined (NAMED_FUNCTIONS)
  433.       fprintf (doc_stream, "   { %s, \"%s\", %s },\n", func, func_name, doc);
  434.       free (func_name);
  435. #else /* !NAMED_FUNCTIONS */
  436.       fprintf (doc_stream, "   { %s, %s },\n", func, doc);
  437. #endif /* !NAMED_FUNCTIONS */
  438.  
  439.       fprintf (funs_stream, "extern void %s ();\n", func);
  440.       free (func);
  441.       free (doc);
  442.     }
  443.   free (buffer);
  444.  
  445.   /* If we created any tags, remember this file on our global list.  Otherwise,
  446.      free the memory already allocated to it. */
  447.   if (block->entries)
  448.     add_pointer_to_array (block, emacs_tags_index, emacs_tags,
  449.               emacs_tags_slots, 10, EMACS_TAG_BLOCK *);
  450.   else
  451.     {
  452.       free (block->filename);
  453.       free (block);
  454.     }
  455. }
  456.  
  457. static void
  458. fatal_file_error (filename)
  459.      char *filename;
  460. {
  461.   fprintf (stderr, "Couldn't manipulate the file %s.\n", filename);
  462.   exit (2);
  463. }
  464.  
  465. static FILE *
  466. must_fopen (filename, mode)
  467.      char *filename, *mode;
  468. {
  469.   FILE *stream;
  470.  
  471.   stream = fopen (filename, mode);
  472.   if (!stream)
  473.     fatal_file_error (filename);
  474.  
  475.   return (stream);
  476. }
  477.  
  478.